package org.footballmanager.pandafootball.system.controller.hy;

import org.footballmanager.pandafootball.common.constants.Constants;
import org.footballmanager.pandafootball.common.util.StringUtil;
import org.footballmanager.pandafootball.system.controller.BaseController;
import org.footballmanager.pandafootball.system.domain.*;
import org.footballmanager.pandafootball.system.form.impl.FootXfSearchForm;
import org.footballmanager.pandafootball.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;

@Controller
@SessionAttributes("footXfSearchForm")
@RequestMapping("/hy/footXf")
public class HyXfController extends BaseController {
    /* 实体名称 */
    private static final String ENTITYNAME = "footXf";
    private static final String ADDPAGE = String.format(Constants.ADDPAGE, Constants.HY, ENTITYNAME);
    private static final String LISTPAGE = String.format(Constants.LISTPAGE, Constants.HY, ENTITYNAME);
    private static final String VIEWPAGE = String.format(Constants.VIEWPAGE, Constants.HY, ENTITYNAME);

    @Autowired
    private FootXfService footXfService;

    @Autowired
    private FootPersonService footPersonService;

    @Autowired
    private FootTeamService footTeamService;

    @Autowired
    private FootTaxService footTaxService;

    @Autowired
    private FootUserPersonService footUserPersonService;

    @Autowired
    private FootUserService footUserService;

    @Autowired
    private FootZjService footZjService;

    /**
     * 初始化
     *
     * @param map              参数
     * @param footXfSearchForm 条件参数
     */
    @ModelAttribute
    public void init(Model map, HttpServletRequest request, @ModelAttribute FootXfSearchForm footXfSearchForm) {
        map.addAttribute("userList", footUserService.getAllWithoutAdmin(request));
        FootXfSearchForm newFootXfSearchForm = new FootXfSearchForm();
        if (footXfSearchForm != null) {
            newFootXfSearchForm = footXfSearchForm;
        }
        map.addAttribute(String.format(Constants.FORMNAME, ENTITYNAME), newFootXfSearchForm);
    }

    @RequestMapping(value = "/search")
    public String search(Model map, HttpServletRequest request, @ModelAttribute FootXfSearchForm footXfSearchForm) {
        FootUser user = (FootUser) request.getSession().getAttribute(Constants.USER);
        footXfSearchForm.setUserId(user.getId());
        footXfService.search(ENTITYNAME, map, footXfSearchForm);
        return LISTPAGE;
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String openAddPage(Model map, HttpServletRequest request) {
        Long userId = (Long) request.getSession().getAttribute(Constants.USERID);
        map.addAttribute("footPersonList", footUserPersonService.findFootPersonByUserId(userId, request));
        FootXf xf = new FootXf();
        xf.setUserId(userId);
        xf.setXfsj(new Date());
        map.addAttribute(ENTITYNAME, xf);
        return ADDPAGE;
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String save(@Valid @ModelAttribute FootXf footXf, BindingResult bindingResult,
                       HttpServletRequest request, SessionStatus s) {
        if (!bindingResult.hasErrors()) {
            FootUser user = (FootUser) request.getSession().getAttribute(Constants.USER);
            footXf.setUserId(user.getId());
            FootUserPerson fup = footUserPersonService.get(footXf.getFupId());
            footUserPersonService.delete(footXf.getFupId(), request);
            FootPerson fp = footPersonService.get(fup.getQyId());
            FootTeam team = footTeamService.getTeamByUserid(user.getId());
            if (fp != null) {
                //返还球员身价的30%
                double result = Double.valueOf(fp.getQysj()) * footTaxService.getDismissTax();
                team.setQdZzj(team.getQdZzj() + result);
                team.setYy("因解约球员" + StringUtil.getFullNameFromPerson(fp) + ",增加" +
                        new DecimalFormat(",###").format(result) + "PB,当前的球队总资金为:" +
                        new DecimalFormat(",###").format(team.getQdZzj()) + "PB") ;
                footTeamService.updateAll(team, request);
            }
            footXf.setXfsj(new Date());
            footXfService.save(footXf, request);
            s.setComplete();
            return "redirect:/hy/footClub/viewList";
        } else
            return ADDPAGE;
    }

    @RequestMapping(value = "/view", method = RequestMethod.GET)
    public String openViewPage(Model map, HttpServletRequest request, @ModelAttribute FootXfSearchForm footXfSearchForm) {
        FootUser user = (FootUser) request.getSession().getAttribute(Constants.USER);
        List<FootXf> list = footXfService.findByUserId(user.getId());
        map.addAttribute(String.format(Constants.LISTENTITY, ENTITYNAME), list);
        return VIEWPAGE;
    }

    /**
     * 续约
     *
     * @param request 请求
     * @param qdId    球员主键
     * @param xycs    续约次数
     * @return 成功或失败
     */
    @RequestMapping(value = "/xy", method = RequestMethod.POST)
    @ResponseBody
    public JsonReturn xuyue(HttpServletRequest request, Long qdId, Integer xycs) {
        FootUser user = (FootUser) request.getSession().getAttribute(Constants.USER);
        return footUserPersonService.renew(user.getId(), qdId, xycs, request);
    }
}
